<?php
	class myPage{
		private $Nums;//总条目
		private $pageSize;//显示的行数
		private $currentPage;//当前页
		private $pageNums;//总页数
		private $subNums;//子页数
		private $subLink;//子页链接
		private $subType;/*子叶类型 支持两种模式 1为普通模式(首页 上一页 下一页 尾页) 2经典模式(首页 上一页 1 2 3 ... 下一页 尾页) */
		private $page_array=array();//构造分页的数组
		public function __construct($Nums,$pageSize,$currentPage,$subNums,$subLink,$subType='1'){
			$this->Nums=intval($Nums);
			$this->pageSize=intval($pageSize);
			if(!$currentPage){
				$this->currentPage=1;
			}else{
				$this->currentPage=intval($currentPage);
			}
			$this->subNums=intval($subNums);
			$this->subLink=$subLink;
			$this->subType=intval($subType);
			$this->pageNums=ceil($this->Nums/$this->pageSize);
		}
		public function __destruct(){
			unset($pageSize);
			unset($Nums);
			unset($currentPage);
			unset($subNums);
			unset($subType);
			unset($subLink);
			unset($page_array);
			unset($pageNums);
		}
		public function show(){
			if($this->subType==1){
				$this->subCssOne();
			}else if($this->subType==2){
				$this->subCssTwo();
			}
		}
		public function initArray(){
			for($i=0;$i<$this->subNums;$i++){
				$this->page_array[$i]=$i;
			}
			return $this->page_array;
		}
		public function construct_num_page(){
			if($this->pageNums <$this->subNums){
				 $current_array=array();
				 for($i=0;$i<$this->pageNums;$i++){
				 	$current_array[$i]=$i+1;
				 }
			}else{
				 	$current_array=$this->initArray();
				 	if($this->currentPage<=3){ /*显示的开始位置是:(当前页-2)*/
				 		for($i=0;$i<count($current_array);$i++){
				 			$current_array[$i]=$i+1;
				 		}
				 	}else if($this->currentPage <=$this->pageNums && $this->currentPage > ($this->pageNums-$this->subNums+1)){/*对最后的子页进行处理*/
				 		for($i=0;$i<count($current_array);$i++){
				 			$current_array[$i]=($this->pageNums)-($this->subNums)+1+$i;
				 		}
				 	}else{
				 		for($i=0;$i<count($current_array);$i++){ /*显示的开始位置:(当前页-2)*/
				 			$current_array[$i]=$this->currentPage-2+$i;
				 		}
				 	}
				 }
				 return $current_array;
		}
			
		public function subCssOne(){
				$style="\"text-decoration:none;border: 1px solid #cccccc;padding:2px 8px; margin:0px 5px;color:#4261DE;\"";
				$arrSubCss='';
				$arrSubCss.='总共有'.$this->Nums.'条记录';
				$arrSubCss.='每页显示'.$this->pageSize.'条';
				$arrSubCss.='当前第'.$this->currentPage.'/'.$this->pageNums.'页';
				$firstPagelink=$this->subLink.'1';
				$lastPagelink=$this->subLink.$this->pageNums;
				if($this->currentPage>1){
					//$firstPagelink=$this->subLink.'1';
					$prePagelink=$this->subLink.($this->currentPage-1);
					$arrSubCss.="<a href=\"{$firstPagelink}\" style={$style} >".'首页'."</a>";
					$arrSubCss.="<a href=\"{$prePagelink}\" style={$style} >".'上一页'."</a>";
				}else{
					$arrSubCss.="<a href=\"{$firstPagelink}\" style={$style}>首页</a>";
					$arrSubCss.="<a href=\"{$firstPagelink}\" style={$style}>上一页</a>";
				}
				if($this->currentPage < $this->pageNums){
					$nextPagelink=$this->subLink.($this->currentPage+1);
					//$lastPagelink=$this->subLink.$this->pageNums;
					$arrSubCss.="<a href=\"{$nextPagelink}\" style=$style >".'下一页'."</a>";
					$arrSubCss.="<a href=\"{$lastPagelink}\" style=$style >".'尾页'."</a>";
				}else{
					$arrSubCss.="<a href=\"{$lastPagelink}\" style=$style >".'下一页'."</a>";
					$arrSubCss.="<a href=\"{$lastPagelink}\" style=$style >".'尾页'."</a>";
				}
				echo $arrSubCss;
		}
		public function subCssTwo(){
				$style="\"text-decoration:none;border: 1px solid #cccccc;padding:2px 8px; margin:0px 5px;color:#4261DE;\"";
				$arrSubCss='';
				$arrSubCss.="<span style={$style}>".$this->Nums.'条'."</span>";
				$arrSubCss.="<span style={$style}>".'当前第'.$this->currentPage.'/'.$this->pageNums.'页'."</span>";
				$firstPagelink=$this->subLink.'1';
				$lastPagelink=$this->subLink.$this->pageNums;
				if($this->currentPage>1){
					//$firstPagelink=$this->subLink.'1';
					$prePagelink=$this->subLink.($this->currentPage-1);
					$arrSubCss.="<a href=\"{$firstPagelink}\" style={$style} >".'首页'."</a>";
					$arrSubCss.="<a href=\"{$prePagelink}\" style={$style} >".'上一页'."</a>";
				}else{
					$arrSubCss.="<a href=\"{$firstPagelink}\" style={$style}>首页</a>";
					$arrSubCss.="<a href=\"{$firstPagelink}\" style={$style}>上一页</a>";
				}

				$arr=$this->construct_num_page();
				for($i=0;$i<count($arr);$i++){
					$tmp=$arr[$i];
					if($tmp==$this->currentPage){
						$arrSubCss.="<span style=\"color:black;padding:2px 8px;margin:0px 5px;\">".$tmp."</span>";
					}else{
						$url=$this->subLink.$tmp;
						$arrSubCss.="<a href=\"{$url}\" style=$style >".$tmp."</a>";
					}
				}
				if($this->currentPage < $this->pageNums){
					$nextPagelink=$this->subLink.($this->currentPage+1);
					//$lastPagelink=$this->subLink.$this->pageNums;
					$arrSubCss.="<a href=\"{$nextPagelink}\" style=$style >".'下一页'."</a>";
					$arrSubCss.="<a href=\"{$lastPagelink}\" style=$style >".'尾页'."</a>";
				}else{
					$arrSubCss.="<a href=\"{$lastPagelink}\" style=$style >".'下一页'."</a>";
					$arrSubCss.="<a href=\"{$lastPagelink}\" style=$style >".'尾页'."</a>";
				}
				echo $arrSubCss;
		}
	}
	//用法如下
	//$page=new Page(50,5,8,5,'test.php?page=',2);
?>